﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDE_FIC.DAO;
using SDE_FIC.Models;
using PagedList;

namespace SDE_FIC.Controllers
{
    public class SugestoesController : Controller
    {
        private DBConnect _db = new DBConnect();

        //
        // GET: /UnidadeCurricular/

        public ActionResult Index(int? pagina)
        {
            int tamanhoPagina = 10;
            int numeroPagina = pagina ?? 1;
            CursoDAO _cursoDAO = new CursoDAO(ref _db);

            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);

            return View("~/Views/Sistema/Sugestoes/Index.cshtml", _sugestoesDAO.All().ToPagedList(numeroPagina, tamanhoPagina));
        }

        //
        // GET: /UnidadeCurricular/Details/5

        public ActionResult Details(int id = 0)
        {
            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
            Sugestoes _sugestoes = new Sugestoes();

            _sugestoes = _sugestoesDAO.LoadById(id);

            if (_sugestoes == null)
            {
                return HttpNotFound();
            }
            return View("~/Views/Sistema/Sugestoes/Details.cshtml", _sugestoes);
        }

        //
        // GET: /UnidadeCurricular/Create

        public ActionResult Create()
        {
            UsuarioDAO _usuarioDAO = new UsuarioDAO(ref _db);

            ViewBag.usuario_idusuario = new SelectList(_usuarioDAO.All().ToList(), "idusuario" , "username");

            return View("~/Views/Sistema/Sugestoes/Create.cshtml");
        }

        //
        // POST: /UnidadeCurricular/Create

        [HttpPost]
        public ActionResult Create(Sugestoes sugestoes)
            {
                SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
                UsuarioDAO _usuarioDAO = new UsuarioDAO(ref _db);

                if (ModelState.IsValid)
                {
                    _sugestoesDAO.Insert(sugestoes);
                    return RedirectToAction("Index");
                }

                ViewBag.usuario_idusuario = new SelectList(_usuarioDAO.All().ToList(), "idusuario", "username", sugestoes.Usuario.IdUsuario);
                return View("~/Views/Sistema/Sugestoes/Create.cshtml", sugestoes);
            }

        //
        // GET: /UnidadeCurricular/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
            Sugestoes _sugestoes = new Sugestoes();

            UsuarioDAO _usuarioDAO = new UsuarioDAO(ref _db);
            Usuario _usuario = new Usuario();
            
            _sugestoes = _sugestoesDAO.LoadById(id);

            if (_sugestoes == null)
            {
                return HttpNotFound();
            }
            ViewBag.usuario_idusuario = new SelectList(_usuarioDAO.All().ToList(), "idusuario", "username", _sugestoes.Usuario.IdUsuario);
            return View("~/Views/Sistema/Sugestoes/Edit.cshtml", _sugestoes);
        }

        //
        // POST: /UnidadeCurricular/Edit/5

        [HttpPost]
        public ActionResult Edit(Sugestoes sugestoes)
        {
            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
            Sugestoes _sugestoes = new Sugestoes();
            UsuarioDAO _usuarioDAO = new UsuarioDAO(ref _db);
            Usuario _usuario = new Usuario();

            if (ModelState.IsValid)
            {
                _sugestoesDAO.Update(sugestoes);
                return RedirectToAction("Index");
            }

            ViewBag.usuario_idusuario = new SelectList(_usuarioDAO.All().ToList(), "idusuario", "username", sugestoes.Usuario.IdUsuario);
            return View("~/Views/Sistema/Sugestoes/Edit.cshtml", _sugestoes);
        }

        //
        // GET: /UnidadeCurricular/Delete/5

        public ActionResult Delete(int id = 0)
        {
            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
            Sugestoes _sugestoes = new Sugestoes();

            _sugestoes = _sugestoesDAO.LoadById(id);

            if (_sugestoes == null)
            {
                return HttpNotFound();
            }
            return View("~/Views/Sistema/Sugestoes/Delete.cshtml", _sugestoes);
        }

        //
        // POST: /UnidadeCurricular/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Sugestoes sugestoes)
        {
            SugestoesDAO _sugestoesDAO = new SugestoesDAO(ref _db);
            Sugestoes _sugestoes = new Sugestoes();     

            if (ModelState.IsValid)
            {
                _sugestoesDAO.Delete(sugestoes);
                return RedirectToAction("Index");
            }

            return View("~/Views/Sistema/Sugestoes/Delete.cshtml", sugestoes);
        }

    //    protected override void Dispose(bool disposing)
    //    {
    //        db.Dispose();
    //        base.Dispose(disposing);
    //    }
    }
}